﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AceMobile.Models;
using AceMobile.Models.Phones;

namespace AceMobile.Controllers
{
    public class PhoneController : Controller
    {
        MobileWebContext db = new MobileWebContext();

        //
        // GET: /Phone/

        public ActionResult Index(int? arrangeType, string producer) 
        {
            /* arrangeType:
             * null: all Product.
             * 1: ViewAmount.
             * 2: CreateDate.
             * 3: SumAmout - Amount
             */
            var device = db.Phones.Include("PhoneOS").Include("PhoneProducer").ToList();
            IEnumerable<Phone> device2;
            device2 = device;
            switch (arrangeType)
            {
                case null:                    
                    break;
                case 1:
                    device2 = device.OrderByDescending(a => a.ViewAmount);
                    break;
                case 2:
                    device2 = device.OrderByDescending(a => a.ManufacturingDate.Ticks);
                    break;
                case 3:
                    device2 = device.OrderByDescending(a => a.SumAmount - a.Amount);
                    break;
                default:
                    break;
            }

            if (producer != null)
            {
                device2 = device2.Where(a => a.PhoneProducer.Name == producer);
            }

            ViewBag.listOS = new SelectList(db.PhoneOSs.ToList(), "ID", "Name");
            ViewBag.listProducer2 = db.PhoneProducers.ToList();
            ViewBag.listProducer = new SelectList(ViewBag.listProducer2, "ID", "Name");
            return View(device2);
        }
        [HttpPost]
        public ActionResult Index(int? listOS, int? listProducer, string p)
        {

            var device = db.Phones.Where(
                a => (
                    (listOS == null ? true : (a.PhoneOS.ID == listOS))
                    && (listProducer == null ? true : (a.PhoneProducer.ID == listProducer))
                )).ToList();
            if (p != null)
	        {
                Phone phone;
                for (int i = 0; i < device.Count; i++)
                {
                    phone = device[i];
                    if (!(phone.Name.ToLower().Contains(p.ToLower()) 
                            || phone.PhoneOS.Name.ToLower().Contains(p.ToLower())
                            || phone.PhoneProducer.Name.ToLower().Contains(p.ToLower())))
                    {
                        device.Remove(phone);
                        i--;
                    }
                }
            }
            ViewBag.listOS = new SelectList(db.PhoneOSs.ToList(), "ID", "Name");
            ViewBag.P = p;
            ViewBag.listProducer = new SelectList(db.PhoneProducers.ToList(), "ID", "Name");
            return View(device);
        }
        public ActionResult Details(int id)
        {
            var device = db.Phones.Find(id);
            device.ViewAmount++;
            db.SaveChanges();
            return View(device);
        }

        public static int getPhoneAmount()
        {
            MobileWebContext db = new MobileWebContext();
            return db.Phones.Count();
        }
    }
}
